package org.budo.dubbo.config.reference;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;

/**
 * @author lmw
 * @see com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler
 */
public class DubboReferenceConfiger implements BeanDefinitionRegistryPostProcessor {
    private static final Logger log = LoggerFactory.getLogger(DubboReferenceConfiger.class);

    private Class<?>[] interfaceTypes = new Class<?>[0];

    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {}

    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        if (null == this.getInterfaceTypes() || this.getInterfaceTypes().length < 1) {
            log.warn("#30 config dubbo reference, interfaceTypes is null or empty");
            return;
        }

        String applicationConfigBeanName = this.getBeanName(beanDefinitionRegistry, ApplicationConfig.class);
        String registryConfigBeanName = this.getBeanName(beanDefinitionRegistry, RegistryConfig.class);
        String consumerConfigBeanName = this.getBeanName(beanDefinitionRegistry, ConsumerConfig.class);

        for (Class<?> interfaceType : this.getInterfaceTypes()) {
            RootBeanDefinition beanDefinition = new RootBeanDefinition();
            beanDefinition.setBeanClass(ReferenceConfig.class);
            beanDefinition.setLazyInit(false);
            beanDefinition.getPropertyValues().addPropertyValue("id", interfaceType.getSimpleName());
            beanDefinition.getPropertyValues().addPropertyValue("interface", interfaceType.getName());

            beanDefinition.getPropertyValues().addPropertyValue("application", new RuntimeBeanReference(applicationConfigBeanName));
            beanDefinition.getPropertyValues().addPropertyValue("consumer", new RuntimeBeanReference(consumerConfigBeanName));
            beanDefinition.getPropertyValues().addPropertyValue("registry", new RuntimeBeanReference(registryConfigBeanName));

            beanDefinitionRegistry.registerBeanDefinition(interfaceType.getSimpleName(), beanDefinition);

            log.info("#52 config dubbo reference, interfaceType=" + interfaceType + ", beanDefinition=" + beanDefinition);
        }
    }

    private String getBeanName(BeanDefinitionRegistry beanDefinitionRegistry, Class<?> beanType) {
        String[] beanDefinitionNames = beanDefinitionRegistry.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            BeanDefinition beanDefinition = beanDefinitionRegistry.getBeanDefinition(beanDefinitionName);
            String beanClassName = beanDefinition.getBeanClassName();
            if (beanClassName.equals(beanType.getName())) {
                return beanDefinitionName;
            }
        }

        log.error("#65 getBeanName return null for " + beanType);
        return null;
    }

    public Class<?>[] getInterfaceTypes() {
        return this.interfaceTypes;
    }

    public void setInterfaceTypes(Class<?>[] interfaceTypes) {
        this.interfaceTypes = interfaceTypes;
    }
}